package com.twu.biblioteca;

import com.twu.biblioteca.command.Command;
import com.twu.biblioteca.command.CommandFactory;
import com.twu.biblioteca.util.InputParser;
import com.twu.biblioteca.util.Messages;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/14/13
 * Time: 9:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaSystem {

    private Scanner scanner;
    private User loggedUser = null;

    public BibliotecaSystem(Scanner scanner) {
        this.scanner = scanner;
    }

    public void run() {
        this.showWelcomeMessage();

        int menuInput = 0;
        do {
            try {
                this.showMenuOptions();

                String select = scanner.nextLine();
                menuInput = InputParser.parseUserSelect(select);

                Command command = CommandFactory.generateCommand(this, menuInput);
                if (command != null) {
                    command.runCommand();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } while (menuInput != 0);
    }

    private void showWelcomeMessage() {
        System.out.println(Messages.Welcome_Message);
    }

    private void showMenuOptions() {
        System.out.println(Messages.Menu_Options);
    }

    public void setScanner(Scanner scanner) {
        this.scanner = scanner;
    }

    public Scanner getScanner() {
        return scanner;
    }

    public boolean isLoggedIn() {
        return this.loggedUser != null;
    }

    public User getLoggedUser() {
        return loggedUser;
    }

    public void setLoggedUser(User loggedUser) {
        this.loggedUser = loggedUser;
    }
}
